<?php

/**
 * illustration actions.
 *
 * @package    espace14
 * @subpackage illustration
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class illustrationActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	
	$results_per_page = 2;
    $first_page=1;  
    $this->pagination = new sfDoctrinePager('illustration', $results_per_page);  
    $this->pagination->setQuery(Doctrine_Core::getTable('illustration')->createQuery('a'));  
    $this->pagination->setPage($request->getParameter('page', $first_page));  
    $this->pagination->init();  
	$this->illustrations = $this->pagination->getResults();
	
	$this->getResponse()->setTitle('Espace 14 | Illustration Clinique');
    // $this->illustrations = Doctrine_Core::getTable('illustration')
      // ->createQuery('a')
      // ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->illustration = Doctrine_Core::getTable('illustration')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->illustration);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new illustrationForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new illustrationForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($illustration = Doctrine_Core::getTable('illustration')->find(array($request->getParameter('id'))), sprintf('Object illustration does not exist (%s).', $request->getParameter('id')));
    $this->form = new illustrationForm($illustration);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($illustration = Doctrine_Core::getTable('illustration')->find(array($request->getParameter('id'))), sprintf('Object illustration does not exist (%s).', $request->getParameter('id')));
    $this->form = new illustrationForm($illustration);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($illustration = Doctrine_Core::getTable('illustration')->find(array($request->getParameter('id'))), sprintf('Object illustration does not exist (%s).', $request->getParameter('id')));
    $illustration->delete();

    $this->redirect('illustration/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	
	 if ($form->isValid())
    {	$file = $form->getValue('url_resource');
		if($file)
		{
			$filename = $file->getOriginalName();
			$extension = $file->getExtension($file->getOriginalExtension());
			$file->save(sfConfig::get('sf_root_dir').sfConfig::get('sf_web_dir_name').sfConfig::get('sf_upload_dir_name').'/'.$filename);
			
			$page = $form->save();
			$page->setUrlResource($filename);
			$page->setDescription($request->getParameter('editor1'));
			$page->setPresentation($request->getParameter('editor2'));
			$form = $page->save();
			
		}
		//$this->redirect('modulesformation/edit?id='.$modules->getId());
		
	  	else
		{
			$page = $form->save();
			$id = $page->getId();
			$q = Doctrine_Query::create()->select()->from('illustration i')->where('i.id = ?',$id);
			$url = $q->fetchOne();
			$url = $url->getUrlResource();
			if ($url)
			{
				$page->setUrlResource($url);
				$page->setDescription($request->getParameter('editor1'));
				$page->setPresentation($request->getParameter('editor2'));
				$form = $page->save();
			}
			else
			{
				$page->setDescription($request->getParameter('editor1'));
				$page->setPresentation($request->getParameter('editor2'));
				$form = $page->save();
			}
			
		}
		$this->redirect('illustration/index');
    }
  }
}
